package com.yufu.baidu.ocr;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.activity.result.ActivityResult;

import com.baidu.ai.base.activity.IDcardCardActivity;
import com.baidu.ai.base.parameter.BaseParameter;
import com.baidu.ai.idcard.OCRIDCardDetectUIActivity;
import com.baidu.ai.idcard.OCRIDCardTakePhotoUIActivity;
import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.getcapacitor.JSObject;
import com.getcapacitor.PermissionState;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.ActivityCallback;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;
import com.yufu.baidu.ocr.ui.camera.CameraActivity;
import com.yufu.baidu.ocr.ui.camera.CameraNativeHelper;
import com.yufu.baidu.ocr.ui.camera.CameraView;

@CapacitorPlugin(name = "BaiduOcr", permissions = {
  @Permission(strings = {android.Manifest.permission.CAMERA}, alias = BaiduOcrPlugin.CAMERA_PERMISSION_ALIAS)})
public class BaiduOcrPlugin extends Plugin {

  static final String CAMERA_PERMISSION_ALIAS = "camera";
  private BaiduOcr implementation;
  private IdCard idCardHandler;
  private BankCard bankCardHandler;

  @PluginMethod
  public void echo(PluginCall call) {
    String value = call.getString("value");
    JSObject ret = new JSObject();
    ret.put("value", implementation.echo(value));
    call.resolve(ret);
  }

  @Override
  public void load() {
    super.load();
    implementation = new BaiduOcr(getActivity());
  }


  @PluginMethod
  public void idCard(PluginCall call) {
    idCardHandler = new IdCard(getActivity(), call);
    startActivityForResult(call, idCardHandler.getIntent(), "idCardResultCallback");
  }

  @PluginMethod
  public void bankCard(PluginCall call) {
    bankCardHandler = new BankCard(getActivity(), call);
    startActivityForResult(call, bankCardHandler.getIntent(), "bankCardResultCallback");
  }

  @ActivityCallback
  private void idCardResultCallback(PluginCall call, ActivityResult result) {
    if (call == null) {
      return;
    }
    Intent data = result.getData();
    if(result.getResultCode() == Activity.RESULT_OK && data!=null){
      String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
      String filePath = FileUtil.getSaveFile(getActivity().getApplicationContext()).getAbsolutePath();
      if (!TextUtils.isEmpty(contentType)) {
        if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
          idCardHandler.recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
        } else if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(contentType)) {
          idCardHandler.recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
        }
      }
    }else{
      call.reject("识别失败");
    }
  }

  @ActivityCallback
  private void bankCardResultCallback(PluginCall call, ActivityResult result) {
    if (call == null) {
      return;
    }
    Intent data = result.getData();
    if(result.getResultCode() == Activity.RESULT_OK && data!=null){
      String filePath = FileUtil.getSaveFile(getActivity().getApplicationContext()).getAbsolutePath();
      bankCardHandler.recBankCard(filePath);
    }else{
      call.reject("识别失败");
    }
  }

  /**
   * 将base64转换成bitmap图片
   *
   * @param string base64字符串
   * @return bitmap
   */
  public Bitmap stringToBitmap(String string) {
    // 将字符串转换成Bitmap类型
    Bitmap bitmap = null;
    try {
      byte[] bitmapArray;
      bitmapArray = Base64.decode(string, Base64.DEFAULT);
      bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bitmap;
  }

}
